package com.design.classroommanagement.api

import com.design.classroommanagement.api.dao.AdminDao
import com.design.classroommanagement.api.dao.BaseDao
import com.design.classroommanagement.api.dao.StudentDao
import com.design.classroommanagement.api.dao.TeacherDao
import com.laughing.lib.base.HBaseModel
import com.laughing.lib.utils.getField

abstract class BaseModel() : HBaseModel() {
    abstract fun getKeyId(): String
}

data class User @JvmOverloads constructor(
    var id: String? = "",
    var password: String? = null,
    var account: String? = null,
    var name: String? = null,
    var sex: String? = null,
    var age: String? = null,
    var telephone: String? = null,
    var college: String? = null,
    var role: Role = Role.admin
) : BaseModel() {
    override fun getKeyId(): String {
        return id.toString()
    }
}


data class Bookmark(
    var id: String? = "",
    var tid: String? = "",
    var name: String? = "",
    var classname: String? = "",
    var progress: Int? = 0,
    var date: String? = ""
) : BaseModel() {
    override fun getKeyId(): String {
        return id.toString()
    }
}

data class Classroom(
    var id: String? = "",
    var name: String? = "",
    var progress: String? = "",
    var tid: String? = "",
    var bookmarkId: String? = ""
) : BaseModel() {
    override fun getKeyId(): String {
        return id.toString()
    }
}

data class ClassRoomStudent(
    var id: Int? = -1,
    var classroomId: String?,
    var studentId: String?
) : BaseModel() {
    override fun getKeyId(): String {
        return id.toString()
    }
}


enum class Role {
    admin, teacher, student
}

fun Role.getDao(): BaseDao<User> {
    return when (this) {
        Role.admin -> AdminDao()
        Role.teacher -> TeacherDao()
        Role.student -> StudentDao()
    }
}

fun BaseModel.toMap(): HashMap<String, Any> {
    val clz = this.javaClass
    val map = HashMap<String, Any>()
    clz.declaredFields.forEach {
        val key = it.name
        val value = getField<Any>(key)
        if (value != null) {
            map[key] = value
        }
    }
    return map
}